jQuery.extend( jQuery.easing,
{
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	}
});

function setupArrows() {
	var i;
	var arrows = "";
	var arrow_count = Math.floor($(window).width() / 80);
	for(i = 0; i < arrow_count; i++) {
		arrows = arrows+'<div class="arrow-large arrow">&nbsp;</div><div class="arrow-small arrow">&nbsp;</div>';
	}
	$("#arrows").append(arrows);
	$(".arrow").each(function(){
		var delay = Math.floor(Math.random() * (0 - 5000 + 1)) + 5000;
		animateArrow($(this), delay);
	});
}

function animateArrow(e, delay) {
	var width = $(window).width();
	var left = Math.floor(Math.random()*width+1);
	var speed = Math.floor(Math.random() * (5000 - 2000 + 1)) + 2000;
	var top = 650;
	
	if(delay) {
		top = top + delay;
	}
	
	$(e).css("left", left).css("top", top).animate({
		top:-150
	}, speed, 'easeOutCirc', function() {
		animateArrow(e);
	});
}

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name) {
			return unescape(y);
		}
	}
}

$(function() {
	setupArrows();
	
	$("#get-it").click(function(){
		var tagline = $("#tagline").val();
		var url = $("#url").val();
		if(tagline  == "") {
			$("#tagline").animate({
				opacity:0.1
			}, 200, function(){
				$("#tagline").animate({
					opacity:1
				}, 200);
			});
		}
		if(url == "") {
			$("#url").animate({
				opacity:0.1
			}, 200, function(){
				$("#url").animate({
					opacity:1
				}, 200);
			});
		}
		if(tagline != "" && url != "") {
		    $.fancybox.open({
		    	content: $("#modal-content").html(),
		    	closeBtn: false,
		    	padding: 15,
		    	maxWidth:400,
		    	afterShow: function() {	    	    
		    	    setCookie("tagline",$("#tagline").val(),1);
		    	    setCookie("url",$("#url").val(),1);
		    	    _gat._getTracker("UA-37839053-1")._trackPageview('/generate');
		    	    		    	    
		    	    $.ajax({
						url: "http://conversionsbox.com/save.php",
						data: { tagline: $("#tagline").val(), url: $("#url").val() }
                    });
		    	    
		    	    var script = '<script type="text/javascript" src="//conversionsbox.googlecode.com/svn/cb.js"></script>\n<script type="text/javascript">function r(f){/in/.test(document.readyState)?setTimeout(\'r(\'+f+\')\',9):f()}; r(function(){new ConversionsBox("'+encodeURIComponent($("#tagline").val())+'","'+$("#url").val()+'");});</script>';
		    	    		    	    
		    	    $("textarea").val(script).select();
		    	    $(".close-modal").click(function(){
		    	        $.fancybox.close();
		    	        return false;
		    	    });
		    	}
		    });
		}
		return false;	
	});
	
	$("#see-it").click(function() {
		var cbox_top = $(window).height() - 210 + $(window).scrollTop();
		var cbox_right = $(document).width() / 2 + 300;
		$("#conversions-box:not(.conversions-box-open)").animate({
			bottom:0
		}, 400);
		$(this).fadeOut(100);
		$("#cb-arrow").addClass('active').attr('style', 'top:'+cbox_top+'px; left:'+cbox_right+'px;');
		setTimeout('$("#cb-arrow").attr("style", "top:'+(cbox_top + 200)+'px; left:'+cbox_right+'px; opacity:0;")', 1000);
		return false;
	});
	
	$('#tagline, #url').placeholder();
});
